const { TreeNode, BinaryTreeTraversal } = require("../template")

function testBinaryTreeTraversal() {
  const traversal = new BinaryTreeTraversal()

  console.log(
    "1. 左斜树遍历测试:",
    (() => {
      // 构建左斜树: 1
      //            /
      //           2
      //          /
      //         3
      const leftSkewed = new TreeNode(1)
      leftSkewed.left = new TreeNode(2)
      leftSkewed.left.left = new TreeNode(3)
      
      return JSON.stringify(traversal.preorder(leftSkewed)) === JSON.stringify([1, 2, 3]) &&
             JSON.stringify(traversal.inorder(leftSkewed)) === JSON.stringify([3, 2, 1]) &&
             JSON.stringify(traversal.postorder(leftSkewed)) === JSON.stringify([3, 2, 1]) &&
             JSON.stringify(traversal.levelOrder(leftSkewed)) === JSON.stringify([[1], [2], [3]])
    })()
  )

  console.log(
    "2. 右斜树遍历测试:",
    (() => {
      // 构建右斜树: 1
      //              \
      //               2
      //                \
      //                 3
      const rightSkewed = new TreeNode(1)
      rightSkewed.right = new TreeNode(2)
      rightSkewed.right.right = new TreeNode(3)
      
      return JSON.stringify(traversal.preorder(rightSkewed)) === JSON.stringify([1, 2, 3]) &&
             JSON.stringify(traversal.inorder(rightSkewed)) === JSON.stringify([1, 2, 3]) &&
             JSON.stringify(traversal.postorder(rightSkewed)) === JSON.stringify([3, 2, 1]) &&
             JSON.stringify(traversal.levelOrder(rightSkewed)) === JSON.stringify([[1], [2], [3]])
    })()
  )

  console.log(
    "3. 只有左子树的树遍历测试:",
    (() => {
      // 构建树:    1
      //           /
      //          2
      //         / \
      //        3   4
      const leftOnly = new TreeNode(1)
      leftOnly.left = new TreeNode(2)
      leftOnly.left.left = new TreeNode(3)
      leftOnly.left.right = new TreeNode(4)
      
      return JSON.stringify(traversal.preorder(leftOnly)) === JSON.stringify([1, 2, 3, 4]) &&
             JSON.stringify(traversal.inorder(leftOnly)) === JSON.stringify([3, 2, 4, 1]) &&
             JSON.stringify(traversal.postorder(leftOnly)) === JSON.stringify([3, 4, 2, 1]) &&
             JSON.stringify(traversal.levelOrder(leftOnly)) === JSON.stringify([[1], [2], [3, 4]])
    })()
  )

  console.log(
    "4. 只有右子树的树遍历测试:",
    (() => {
      // 构建树:    1
      //             \
      //              2
      //             / \
      //            3   4
      const rightOnly = new TreeNode(1)
      rightOnly.right = new TreeNode(2)
      rightOnly.right.left = new TreeNode(3)
      rightOnly.right.right = new TreeNode(4)
      
      return JSON.stringify(traversal.preorder(rightOnly)) === JSON.stringify([1, 2, 3, 4]) &&
             JSON.stringify(traversal.inorder(rightOnly)) === JSON.stringify([1, 3, 2, 4]) &&
             JSON.stringify(traversal.postorder(rightOnly)) === JSON.stringify([3, 4, 2, 1]) &&
             JSON.stringify(traversal.levelOrder(rightOnly)) === JSON.stringify([[1], [2], [3, 4]])
    })()
  )

  console.log(
    "5. this上下文遍历测试:",
    (() => {
      const traversalObj = {
        multiplier: 2,
        traversal: new BinaryTreeTraversal(),
        processTraversal: function(root) {
          const result = this.traversal.preorder(root)
          return result.map(value => value * this.multiplier)
        }
      }
      const testTree = new TreeNode(1)
      testTree.left = new TreeNode(2)
      testTree.right = new TreeNode(3)
      const result = traversalObj.processTraversal(testTree)
      return JSON.stringify(result) === JSON.stringify([2, 4, 6])
    })()
  )

  console.log(
    "6. 复杂对象this绑定遍历测试:",
    (() => {
      const traversalConfigObj = {
        config: { offset: 5 },
        traversal: new BinaryTreeTraversal(),
        processWithConfig: function(root) {
          const result = this.traversal.inorder(root)
          return result.map(value => value + this.config.offset)
        }
      }
      const testTree = new TreeNode(1)
      testTree.left = new TreeNode(2)
      testTree.right = new TreeNode(3)
      const result = traversalConfigObj.processWithConfig(testTree)
      return JSON.stringify(result) === JSON.stringify([7, 6, 8]) // [2+5, 1+5, 3+5]
    })()
  )
}

testBinaryTreeTraversal()
